let List = [];

export default {
  //添加购物车
  createCartList: (config) => {
    const { id, imgurl, isSelect, title, color, size, num, price } = JSON.parse(
      config.body
    );
    const idIndex = List.findIndex((item) => item.id == id);
    if (idIndex != -1) {
      List[idIndex].num += num;
    } else {
      List.unshift({
        id,
        isSelect,
        imgurl,
        title,
        color,
        size,
        num,
        price,
      });
    }

    return {
      code: 200,
      msg: "success",
      data: {
        List,
      },
    };
  },
  //获取购物车
  getCartList: () => {
    return {
      code: 200,
      msg: "success",
      data: List,
    };
  },
  //更新购物车勾选状态
  updataChecked(config) {
    const { skuId, isSelect } = JSON.parse(config.body);
    List.some((item) => {
      if (item.id == skuId) {
        item.isSelect = isSelect;
        return true;
      }
    });
    return {
      code: 200,
      msg: "success",
      data: List,
    };
  },
  //更新所有购物车勾选状态
  updataAllChecked:config=> {
    const { isSelect } = JSON.parse(config.body);
    List.forEach((item) => {
      item.isSelect = isSelect;
      return true;
    });
    return {
      code: 200,
      msg: "success",
      data: List,
    };
  },
  //删除购物车商品
  delCartList:config=>{
    const { skuId } = JSON.parse(config.body);
    List = List.filter(item=>{
      if(item.id != skuId){
        return true
      }else{
        return false
      }
    })
    return {
      code: 200,
      msg: "success",
      data: List,
    };
  },
};
